//
//  VisterView.swift
//  SinaWebo
//
//  Created by apple on 16/6/8.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

protocol buttonAction: NSObjectProtocol {
    func userWillLgoin()
    func userWillRegister()
}

class VisterView: UIView {
    
    weak var delegate: buttonAction?

    func setUpUIWithInfo(imageName: String?, textStr: String?) {
        if let name = imageName {
            circly.image = UIImage(named: name)
            warningText.text = textStr
            home.hidden = true
            self.bringSubviewToFront(circly)
        }else{
            startAnimation()
        }
        
    }
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        //设置动画事件
        anim.duration = 20
        //当动画效果完成 / 视图处于非活跃状态下 动画效果不移除图层
        anim.removedOnCompletion = false
        //将基础动画添加到图层
        circly.layer.addAnimation(anim, forKey: nil)
    }
//view 手动 的入口
  override init(frame: CGRect) {
        super.init(frame: frame)
    
    }

    //xib。storyboard 加载view时调用
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
  }
    override func layoutSubviews() {
        super.layoutSubviews()
        setUp()
        setActionForBtn()
    }
    
    func login() {
        if delegate != nil {
            delegate?.userWillLgoin()
        }
    }
    
    func register() {
        if delegate != nil {
            delegate?.userWillRegister()
        }
    }
    
    func setActionForBtn() {
        loginBtn.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
    }
    
    private func setUp() {
        addSubview(circly)
        addSubview(maview)
        addSubview(home)
        addSubview(warningText)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
                //VFL苹果原生自动布局 父视图添加约束
//        translatesAutoresizingMaskIntoConstraints = false
//       addConstraint(NSLayoutConstraint(item: circly, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//       addConstraint(NSLayoutConstraint(item: circly, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //snapKit 添加约束
       circly.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        home.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circly)
        }
        
        warningText.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circly.snp_bottom).offset(16)
            make.leading.equalTo(circly.snp_leading).offset(-20)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(warningText.snp_bottom).offset(10)
            make.left.equalTo(circly).offset(-20)
            make.width.equalTo(80)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(loginBtn.snp_centerY)
            make.left.equalTo(loginBtn.snp_right).offset(80)
            make.width.equalTo(80)
        }
        
        maview.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(loginBtn.snp_bottom)
        }
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
    private lazy var circly: UIImageView = { let img =  UIImage(named:"visitordiscover_feed_image_smallicon")!
       return UIImageView(image: img) }()
    private lazy var home: UIImageView = {  let img = UIImage(named: "visitordiscover_feed_image_house")!
       return UIImageView(image: img)}()
//蒙班
    private lazy var maview: UIImageView = { let img =  UIImage(named:"visitordiscover_feed_mask_smallicon")!
        return UIImageView(image: img) }()

    //label
    private lazy var warningText: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.lightGrayColor()
       // label.font = UIFont().fontWithSize(14)
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.sizeToFit()
        return label }()

    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
       
        let img = UIImage(named: "common_button_white_disable")!
        let stretchX = Int(img.size.width * 0.5)
        let stretchY = Int(img.size.height * 0.5)
        btn.setBackgroundImage(img.stretchableImageWithLeftCapWidth(stretchX, topCapHeight: stretchY), forState: .Normal)
                     return btn }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let img = UIImage(named: "common_button_white_disable")!
        let stretchX = Int(img.size.width * 0.5)
        let stretchY = Int(img.size.height * 0.5)
        btn.setBackgroundImage(img.stretchableImageWithLeftCapWidth(stretchX, topCapHeight: stretchY), forState: .Normal)
        return btn }()
}
